import Vue from 'vue'
import VueRouter from 'vue-router'
import Storage from '@/utils/storageUtils'
import {Message} from "element-ui"

Vue.use(VueRouter)

const routes = [
  {
    path: "/login",
    name: "Login",
    component: () => import('../components/Login')
  }, {
    path: "/register",
    name: "Register",
    component: () => import('../components/Register')
  },
  {
    path: '/',
    name: 'Home',
    component: () => import('../components/Home'),
    children: [
      {
        path: "/",
        name: "Index",
        component: () => import('../components/Index')
      }, {
        path: "/index",
        redirect: "/"
      }, {
        path: "/asset-department",
        name: "Department",
        component: () => import('../components/department/Department')
      }, {
        path: "/asset-person",
        name: "Person",
        component: () => import('../components/person/Person')
      }, {
        path: "/asset-edit-person",
        name: "EditPerson",
        component: () => import('../components/person/EditPerson')
      }, {
        path: "/asset-category",
        name: "AssetCategory",
        component: () => import('../components/assetCategory/AssetCategory')
      }, {
        path: "/asset-brand",
        name: "AssetBrand",
        component: () => import('../components/assetBrand/AssetBrand')
      }, {
        path: "/gaining-method",
        name: "GainingMethod",
        component: () => import('../components/gainingMethod/GainingMethod')
      }, {
        path: "/asset-provider",
        name: "AssetProvider",
        component: () => import('../components/assetProvider/AssetProvider')
      }, {
        path: "/asset-storage",
        name: "AssetStorage",
        component: () => import('../components/assetStorage/AssetStorage')
      }, {
        path: "/asset-discard-mode",
        name: "AssetDiscardMode",
        component: () => import('../components/assetDiscardMode/AssetDiscardMode')
      }, {
        path: "/asset-user",
        name: "AssetUser",
        component: () => import('../components/assetUser/AssetUser')
      }, {
        path: "/asset-device-use",
        name: "AssetDeviceUse",
        component: () => import('../components/assetDeviceUse/AssetDeviceUse')
      }, {
        path: "/asset-provider-edit",
        name: "AssetProviderEdit",
        component: () => import('../components/assetProvider/AssetProviderEdit')
      }
    ]
  },
]

const router = new VueRouter({
  mode: "history",
  base: "asset-manager",
  routes
})

router.beforeEach((to, from, next) => {
  let user = Storage.Local.get("user");
  if (!user) {
    //用户没有登录
    //访问登录页面 直接放行
    if (to.path.indexOf("/login") != -1) {
      next();
    } else {
      //访问其他页面 直接跳转为登录页面
      router.push({
        path: "/login",
        name: "Login"
      });
      Message.error("您还未登录，请先登录");
    }
  } else {
    next();
  }
})

export default router
